<?php
require 'include/library.inc.php';
require 'include/paging.inc.php';

$course_type_id = escape($_GET['course_type_id']);
$country_id = escape($_GET['country_id']);
$city_id = escape($_GET['city_id']);
$duration = escape($_GET['duration']);

$sql = "SELECT
			s.school_id,
			s.name AS school_name,
			s.logo,
			s.sef_url,
			s.order_number,
			c.name AS city,
			c.state,
			d.name AS country,
				(SELECT MIN(tuition) 
				FROM course_dates AS co,
					 courses AS c
				WHERE s.school_id = c.school_id
				AND	c.course_id = co.course_id
				AND c.published
				) AS tuition
		FROM
		  	schools AS s,
			cities AS c,
			countries AS d
			";
			
if ($course_type_id || $duration) {
	$sql .= ",
			courses AS cou
			";
}

if ($duration) {
	$sql .= ",
			course_dates AS cd
			";
}

$sql .= "
		WHERE
			s.city_id = c.city_id
		AND
			c.country_id = d.country_id
		AND
			s.published
		  ";
		  
if ($country_id) {
	$sql .= "
		AND
			d.country_id = ".$country_id."
			";
}

if ($city_id) {
	$sql .= "
		AND
			c.city_id = ".$city_id."
			";
}

if ($course_type_id || $duration) {
	$sql .= "
		AND
			cou.school_id = s.school_id
		AND
			cou.published
			";
}

if ($course_type_id) {
	$sql .= "
		AND 
			cou.course_type_id = ".$course_type_id."
			";
}

if ($duration) {
	$sql .= "
		AND
			cou.course_id = cd.course_id
		AND 
			cd.duration >= ".$duration."
			";
}

$sql .= "
		GROUP BY
			s.school_id
		  ";
		  
if (!isset($_GET['sort'])) $_GET['sort'] = 1;
if (!isset($_GET['sortBy'])) $_GET['sortBy'] = 5;

$paging = new Paging($sql, $_GET['sortBy'], $_GET['sort'], array(), 6);
$result = $paging->query();


head("Search English Language Courses and Programs","Find English language courses and Programs to study English abroad. Enroll in TOEFL preparation or study Business English in the United States, the United Kingdom or Canada",100);
?>
      
    	<div id="left">
        	<h2>English Language Schools</h2>
            
            <div class="padding" style="min-height:400px;">
            <?php
			
			$i=1;
			while ($row = mysql_fetch_object($result)) {				  
			?>
            	
                <div class="school">
                	<div class="img">
                    	<a href="school/<?=$row->sef_url?>">
                    		<img src="<?=HTML_BASE.'uploads/logos/'.$row->logo?>" alt="<?=$row->school_name?>" height="100" width="100" />
                        </a>
                    </div>
                    <h4><a href="school/<?=$row->sef_url?>"><?=$row->school_name?></a></h4>
                    <div class="address">
						<?=$row->city?>, <?=$row->state?> <?=$row->country?>
                    </div>
                    <div class="reviews">
                    	<?=get_school_reviews($row->school_id) ?>
                    </div>
                    <a class="button" href="school/<?=$row->sef_url?>">Select Courses</a>
                    
                    <div class="tuition">
						starting from <span><?=$_SESSION["curr_sim"]?><?=round($row->tuition*$_SESSION["currency_value"])?></span>
                    </div>
                </div>
                
                <div class="separator">
                </div>
                
            <?php
            	$i++;
			}
			?>
            
            <div class="pagingwrap"><div class="paging">
				<?=$paging->display()?>
            </div></div>
            
            </div>
        </div>
                
        <div class="right">
        	<h2>Countries</h2>
            
            <div class="padding">
            	<ul>
                <?php
          
                  $sql = "SELECT
				  		c.country_id,
						d.name AS country
					FROM
						cities AS c,
						countries AS d
					WHERE
						c.country_id = d.country_id
					GROUP BY
						c.country_id
					  ";
					$result = query($sql);
					while ($row_country = mysql_fetch_object($result)) {	  
                ?>
            		
                	<li <?=($row_country->country_id==$country_id)?'class="active"':''?> >
                    <a rel="nofollow" href="schools.php?country_id=<?=(($row_country->country_id==$country_id)?'0':$row_country->country_id)?>&course_type_id=<?=$course_type_id?>" ><?=$row_country->country?></a>
                    </li>
                    
                <?php
                    } 
                 ?>	
                 </ul>
            </div>
        </div>
        
        <?php if($country_id) { ?>
        
        <div class="right">
        	<h2>Cities</h2>
            
            <div class="padding">
            	<ul>
                <?php
          
                  $sql = "SELECT
				  		city_id,
						name
					FROM
						cities
					WHERE
						country_id = ".$country_id."
					  ";
					$result = query($sql);
					while ($row_city = mysql_fetch_object($result)) {	  
                ?>
            		
                	<li <?=($row_city->city_id==$city_id)?'class="active"':''?> >
                    <a rel="nofollow" href="schools.php?country_id=<?=$country_id?>&course_type_id=<?=$course_type_id?>&city_id=<?=(($row_city->city_id==$city_id)?'0':$row_city->city_id)?>" ><?=$row_city->name?></a>
                    </li>
                    
                <?php
                    } 
                 ?>	
                 </ul>
            </div>
        </div>
        
        <?php } ?>
        
        <div class="right">
        	<h2>Course type</h2>
            
            <div class="padding">
            	<ul>
                <?php
          
                  $sql = "SELECT
				  		course_type_id,
						name
					FROM
						course_types
					  ";
					$result = query($sql);
					while ($row_type = mysql_fetch_object($result)) {	  
                ?>
            		
                	<li <?=($row_type->course_type_id==$course_type_id)?'class="active"':''?> >
                    <a rel="nofollow" href="schools.php?country_id=<?=$country_id?>&course_type_id=<?=(($row_type->course_type_id==$course_type_id)?'0':$row_type->course_type_id)?>&city_id=<?=$city_id?>" ><?=$row_type->name?></a>
                    </li>
                    
                <?php
                    } 
                 ?>	
                 </ul>
            </div>
        </div>
        
        <div class="right">
        <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
        <!-- ads300x250 -->
        <ins class="adsbygoogle"
             style="display:inline-block;width:300px;height:250px"
             data-ad-client="ca-pub-4777399669949031"
             data-ad-slot="4162206613"></ins>
        <script>
        (adsbygoogle = window.adsbygoogle || []).push({});
        </script>
        </div>
   
    
<?php foot(); ?>